<template>
    <div>

        <el-dialog title="代售我的商家" append-to-body close-on-click-modal :visible.sync="daishouwodeShow" width="960px">
            <div class="def-rise">
                商家统一提号折扣: <span class="rise-num">{{ defaultRise.configValue }}</span>折
                <el-button type="text" @click="setRice(null, 2)">设置统一折扣</el-button>
            </div>
            <div style="margin-bottom:15px;font-size:16px;text-align:center;line-height:30px">
                <el-table border stripe :data="daishouwode">
                    <!-- <el-table-column type="selection" width="55">
            </el-table-column> -->
                    <el-table-column align="center" label="商家" prop="tenantName">
                    </el-table-column>
                    <el-table-column align="center" show-overflow-tooltip label="代售游戏" prop="gameNames">

                    </el-table-column>
                    <el-table-column align="center" label="提号折扣" prop="rise">
                        <template slot-scope="scope">
                            <span v-if="scope.row.rise">
                                <span style="color: #f40;font-size: 18px;">{{ scope.row.rise }}</span>
                                折 </span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" label="操作">
                        <template slot-scope="scope">
                            <el-button type="primary" size="mini" @click="setRice(scope.row, 1)">设置折扣</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>

        </el-dialog>
        <!-- 设置折扣的弹窗 -->
        <el-dialog title="设置折扣" :visible.sync="discountDialogVisible" width="500px" append-to-body>
            <el-form :model="discountForm" ref="discountForm" :rules="discountRules" label-width="80px">
                <el-form-item label="商家" v-if="visibleType == 1">
                    {{ currentRow.tenantName }}
                </el-form-item>
                <el-form-item label="提号折扣" prop="rise">
                    <el-input-number v-model="discountForm.rise" placeholder="请输入折扣价格" :precision="1" :step="1" :min="0"
                        :max="10" :controls="true" style="width: 94%">
                    </el-input-number>
                    <span style="margin-left: 5px;">折</span>
                </el-form-item>
            </el-form>

            <div slot="footer" class="dialog-footer">
                <div v-if="visibleType == 1" class="tips">注:当前商家在系统提号下单时,按照设置的提号折扣自动计算折扣价</div>
                <div v-else class="tips">注:统一折扣在未单独设置商家折扣时启用</div>
                <el-button @click="discountDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveDiscount">保 存</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>

// homeApi.setRice   设置折扣
import goodsListApi from "@/api/gameConfig/goodsList";
import customerServiceApi from "@/api/pickUp/customerService";
import { saveShopParms } from "@/api/system/shopManage";
import homeApi from "@/api/home/newIndex";
export default {
    name: "comp8Dialog",
    computed: {
        shopInfo() {
            return this.$store.getters.shopInfo;
        },
    },
    data() {
        return {
            daishouwodeShow: false,
            visibleType: 1, // 1为单独设置折扣 2为默认折扣
            defaultRise: {
                configValue: 10,
                configKey: 'businessRise',
            }, // 默认折扣

            daishouwodeShow: false,
            daishouwode: [],
 discountDialogVisible: false,
      discountForm: {
        buyTenantId: '',
        sellTenantId: '',
        rise: 10
      },
      discountRules: {
        rise: [
          {required: true, message: '请输入折扣价格', trigger: 'blur'},
          {
            pattern: /^(10(\.0{1,2})?|([0-9])(\.\d{1,2})?)$/,
            message: '请输入有效的折扣价格，最多两位小数',
            trigger: 'blur'
          }
        ]
      },
      currentRow: {tenantName: ''}
        };
    },
    methods: {
        open() {
            this.getDefaultRise()
            this.getDaishouwode()
        },
        // 获取默认折扣
        async getDefaultRise() {
            const res = await customerServiceApi.getConfigByTenantId({
                configKey: "businessRise",
                tenantId: this.shopInfo.tenantId
            })
            if (!res.data) return
            this.defaultRise = res.data

        },
        // 获取代售我的商家列表
        getDaishouwode() {
            goodsListApi.getDaishouwode().then((res) => {
                this.daishouwode = res.data.filter(e => e.tenantId != this.shopInfo.tenantId)
                this.daishouwodeShow = true;
            });
        },


        // 打开设置折扣弹窗
        setRice(row, type) {
            if (type == 1) {
                // 单独设置处理
                this.currentRow = row;
                this.discountForm.rise = row.rise || 10;
            } else {
                this.discountForm.rise = this.defaultRise.configValue
            }
            this.visibleType = type;
            this.discountDialogVisible = true;
        },

        // 保存折扣
        saveDiscount() {
            this.$refs.discountForm.validate(valid => {
                if (valid) {
                    // 构建请求参数
                    let params = {};
                    if (this.visibleType == 1) {
                        params = {
                            buyTenantId: this.currentRow.tenantId,
                            sellTenantId: this.shopInfo.tenantId,
                            rise: this.discountForm.rise
                        };
                    } else {
                        this.defaultRise.configValue = this.discountForm.rise
                        params = this.defaultRise
                    }
                    const setBusinessRise = (type, $this) => {
                        if (type == 1) {
                            return homeApi.setRice(params)
                        } else {
                            return saveShopParms(params)
                        }
                    }
                    setBusinessRise(this.visibleType).then(res => {
                        if (res.code === 200) {
                            this.$message.success('设置折扣成功');
                            this.discountDialogVisible = false;
                            // 更新列表中的折扣值
                            this.currentRow.rise = this.discountForm.rise;
                            this.defaultRise.id = res.data
                            // 刷新数据
                            this.getDaishouwode();
                        } else {
                            this.$message.error(res.msg || '设置折扣失败');
                        }
                    }).catch(error => {
                        this.$message.error('设置折扣失败');
                        console.error(error);
                    });
                }
            });
        },
    },
    mounted() {
        this.defaultRise.tenantId = this.shopInfo.tenantId
    },
};

</script>


<style scoped lang="scss">
.tips {
  color: red;
  font-size: 12px;
  text-align: center;
  margin-bottom: 10px;
}

::v-deep .el-dialog__body {
  padding-bottom: 0px !important;
}



.def-rise {
  font-size: 20px;
  font-weight: bold;
  margin: 10px 0;

  .rise-num {
    color: red;
    font-size: 22px;
    margin: 0 10px;
    font-family: "华文中宋", sans-serif;
  }
}</style>